package dao.implementacao.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import dao.ConnectionPool;
import dao.FabricaDAO;
import dao.assinatura.IProfessorDAO;

import model.Aluno;
import model.Professor;

public class ProfessorDAO implements IProfessorDAO
{
	private DataSource dataSource;
	
	public ProfessorDAO() {
		ConnectionPool connectionPool = FabricaDAO.createCoonectionPool();
		this.dataSource = connectionPool.getDataSource();
	}
	
	
	@Override
	public void addProfessor(Professor professor) 
	{
		try 
        {  
			Connection con = dataSource.getConnection();
		 	String sql = "insert into professor(cod_prof,nome,endereco,cpf,telefone,cod_ins_prof,login,senha) values(?,?,?,?,?,?,?,?)";
		 	PreparedStatement comando = con.prepareStatement(sql);
		 	comando.setInt(1, professor.getCodigo());
            comando.setString(2, professor.getNome());
            comando.setString(3, professor.getEndereco());
            comando.setString(4, professor.getCpf());
            comando.setString(5, professor.getTelefone());
            comando.setInt(6, professor.getInstituicao());
            comando.setString(7, professor.getLogin());
            comando.setString(8, professor.getSenha());    
            comando.execute();
            comando.close();
        } catch (Exception e) 
        {  
        	e.printStackTrace();
        }
		
	}

	@Override
	public void updateProfessor(Professor professor) 
	{
		try 
        {  
			Connection con = dataSource.getConnection();
			String sql = "update professor set nome=? , endereco = ?, cpf = ?, telefone = ?,cod_ins_prof = ?,login=?,senha=? where cod_prof=?";
		 	PreparedStatement comando = con.prepareStatement(sql);
            comando.setString(1, professor.getNome());
            comando.setString(2, professor.getEndereco());
            comando.setString(3, professor.getCpf());
            comando.setString(4, professor.getTelefone());
            comando.setInt(5, professor.getInstituicao());
            comando.setString(6, professor.getLogin());
            comando.setString(7, professor.getSenha());
            comando.setInt(8, professor.getCodigo());
            comando.execute();
            comando.close();
        } catch (Exception e) 
        {  
        	e.printStackTrace();
        }	
		
	}

	@Override
	public void deleteProfessor(Professor professor) 
	{
		try 
        {  
			Connection con = dataSource.getConnection();
		 	String sql = "delete from professsor where cod_prof=?";
		 	PreparedStatement comando = con.prepareStatement(sql);
            comando.setInt(1, professor.getCodigo());
            comando.execute();
            comando.close();
        } catch (Exception e) 
        {  
        	e.printStackTrace();
        }	
		
	}

	@Override
	public Professor getProfessorById(int id)
	{
		Professor retorno = null;
		
		try 
		{		
			Connection con = dataSource.getConnection();
			String sql = "select * from professor where cod_prof ="+id;
			
			Statement s = con.createStatement();	
			ResultSet rs = s.executeQuery(sql);	
			
			if(rs.next()){
				int codigo = rs.getInt("cod_prof");
				String nome = rs.getString("nome");
				String endereco = rs.getString("endereco");
				String cpf = rs.getString("cpf");
				String telefone = rs.getString("telefone");
				String login = rs.getString("login");
				String senha = rs.getString("senha");
				
				retorno = new Professor(codigo,nome,endereco,cpf,telefone,login,senha);
			}
			
			s.close();
			rs.close();
			
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}	
		
		return retorno;
	}

	@Override
	public List<Professor> getProfessores() 
	{
		List<Professor> professor = new ArrayList<Professor>();
		try 
		{		
			Connection con = dataSource.getConnection();
			String sql = "select * from professor";
			
			Statement s = con.createStatement();	
			ResultSet rs = s.executeQuery(sql);	
			
			while(rs.next())
			{
				int codigo = rs.getInt("cod_prof");
				String nome = rs.getString("nome");
				String endereco = rs.getString("endereco");
				String cpf = rs.getString("cpf");
				String telefone = rs.getString("telefone");
				String login = rs.getString("login");
				String senha = rs.getString("senha");
				
				professor.add(new Professor(codigo,nome,endereco,cpf,telefone,login,senha));
			}
			
			s.close();
			rs.close();
			
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}	
		
		return professor;
	}

	@Override
	public void finalizaConexaoProfessor() {
		try {
			Connection con = dataSource.getConnection();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
